package de.herberlin.colormatcher;

import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.BevelBorder;

/**
 * Class shows a RGB color.
 * @author hans joachim herbertz
 * created 11.07.2003
 */
public class Spot extends JPanel implements MouseListener {

	private int preferredSize = 80;
	private JTextField textField = null;

	public Spot(RGB rgb) {
		init();
		changeColor(rgb);
	}
	private void init() {
		setLayout(new FlowLayout());
		addMouseListener(this);
		this.setBorder(
			BorderFactory.createBevelBorder(
				BevelBorder.LOWERED,
				Color.lightGray,
				Color.darkGray));
		textField = new JTextField();
		textField.setFont(new Font("Monospaced",Font.PLAIN,12));
		textField.addMouseListener(this);
		textField.setBorder(null);
	}
	private void changeColor(RGB rgb) {
		this.removeAll();
		this.setBackground(rgb.getColor());
		textField.setText(rgb.toString());
		textField.setEditable(false);
		textField.setBackground(rgb.getColor());
		float[] hsb = Color.RGBtoHSB(rgb.r, rgb.g, rgb.b, null);
		if (hsb[2] < .6) {
			textField.setForeground(Color.white);
		} else {
			textField.setForeground(Color.black);
		}
		add(textField, BorderLayout.CENTER);
	}
	public void setRGB(RGB newColor) {
		changeColor(newColor);
	}
	public Color getTextColor() {
		return textField.getForeground();
	}
	public Color getColor() {
		return new RGB(textField.getText()).getColor();
	}
	public String getRGBString() {
		return textField.getText();
	}

	public Dimension getPreferredSize() {
		return new Dimension(preferredSize, preferredSize);
	}
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		Container c = frame.getContentPane();
		c.setLayout(new FlowLayout());
		c.add(new Spot(new RGB("242744")));
		c.add(new Spot(new RGB("ff00bb")));
		c.add(new Spot(new RGB("abc012")));
		frame.pack();
		frame.show();
	}

	private void showPopup(MouseEvent e) {
		if (e.isPopupTrigger()) {
			JPopupMenu menu = new JPopupMenu();
			menu.add(new AbstractAction(Colormatcher.getBundle().getString("Copy")) {
				public void actionPerformed(ActionEvent e) {
					StringSelection stringSel =
						new StringSelection(textField.getText());
					Toolkit
						.getDefaultToolkit()
						.getSystemClipboard()
						.setContents(
						stringSel,
						stringSel);
				}
			});
			menu.show(this, e.getX(),e.getY());
		}
	}
	/**
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	public void mouseClicked(MouseEvent arg0) {
	}
	/**
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	public void mousePressed(MouseEvent arg0) {
		showPopup(arg0);
	}
	/**
	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	public void mouseReleased(MouseEvent arg0) {
		showPopup(arg0);
	}
	/**
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	public void mouseEntered(MouseEvent arg0) {
	}
	/**
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	public void mouseExited(MouseEvent arg0) {
	}
}
